package com.jeeplus.modules.guide.utils;

import com.jeeplus.modules.guide.model.constant.GuideConstant;
import net.sf.json.JSONArray;
import net.sf.json.JSONObject;

import java.text.SimpleDateFormat;
import java.util.Date;


/**
 * 高德地图导航工具类
 *
 * @author hanKongbin
 * @编写时间 2018年8月14日 上午10:01:05
 * @文件名 MapNavUtil.java
 * @类名 MapNavUtil
 */
public class MapNavUtil {
    private String startCoordinate;
    private String endCoordinate;
    private String applicationKey;
    private String param;

    /**
     * 必须要构造参数
     *
     * @param startCoordinates 起点经纬度 经度在前，纬度在后
     * @param endCoordinates   终点经纬度 经度在前，纬度在后
     * @param applicationKey   高德地图应用key，需要Web服务类型的key
     */
    public MapNavUtil(Double[] startCoordinates, Double[] endCoordinates,
                      String applicationKey) throws Exception {
        if (startCoordinates.length < 2 && endCoordinates.length < 2) {
            throw new Exception("经纬度必填，且必须合法！");
        }
        this.startCoordinate = startCoordinates[0] + "," + startCoordinates[1];
        this.endCoordinate = endCoordinates[0] + "," + endCoordinates[1];
        this.applicationKey = applicationKey;
        this.param = ("origin=" + this.startCoordinate + "&destination=" + this.endCoordinate + "&key=" + this.applicationKey).replaceAll(" ", "");

    }

    /**
     * 驾车导航
     *
     * @param key 时长/距离
     * @return
     */
    public String getDriving(String key) {
        String sendGet = MapNavHttpRequest.sendPost("https://restapi.amap.com/v3/direction/driving", param);
        JSONObject jsonObject = JSONObject.fromObject(sendGet);
        String routeJsonString = jsonObject.get("route").toString();
        JSONObject routeObject = JSONObject.fromObject(routeJsonString);
        JSONArray jsonArray = routeObject.getJSONArray("paths");
        JSONObject zuiJson = jsonArray.getJSONObject(0);
        return zuiJson.get(key).toString();
    }

    /**
     * 步行导航 距离/时长
     *
     * @param key
     * @return
     */
    public String getWalking(String key) {
        String sendGet = MapNavHttpRequest.sendPost("https://restapi.amap.com/v3/direction/walking", param);
        JSONObject jsonObject = JSONObject.fromObject(sendGet);
        String routeJsonString = jsonObject.get("route").toString();
        JSONObject routeObject = JSONObject.fromObject(routeJsonString);
        JSONArray jsonArray = routeObject.getJSONArray("paths");
        JSONObject zuiJson = jsonArray.getJSONObject(0);
        return zuiJson.get(key).toString();
    }


    /**
     * 骑行导航
     *
     * @param key 距离/时长
     * @return
     */
    public String getBicycling(String key) {
        String sendGet = MapNavHttpRequest.sendPost("https://restapi.amap.com/v4/direction/bicycling", param);
        JSONObject jsonObject = JSONObject.fromObject(sendGet);
        String routeJsonString = jsonObject.get("data").toString();
        JSONObject routeObject = JSONObject.fromObject(routeJsonString);
        JSONArray jsonArray = routeObject.getJSONArray("paths");
        JSONObject zuiJson = jsonArray.getJSONObject(0);
        return zuiJson.get(key).toString();
    }

    /**
     * 公交车导航
     *
     * @param key 距离/时长
     * @return
     */
    public String getIntegrated(String key) {
        SimpleDateFormat sd = new SimpleDateFormat("yyyy-MM-dd");
        SimpleDateFormat sd2 = new SimpleDateFormat("HH:mm");
        Date date = new Date();
        this.param = (this.param + "&city=四川&cityd=四川&strategy=0&nightflag=0&date=" + sd.format(date) + "&time=" + sd2.format(date));
        this.param = this.param.replaceAll(" +", "");
        String sendGet = MapNavHttpRequest.sendPost("https://restapi.amap.com/v3/direction/transit/integrated", this.param);
        JSONObject jsonObject = JSONObject.fromObject(sendGet);
        String routeJsonString = jsonObject.get("route").toString();
        JSONObject routeObject = JSONObject.fromObject(routeJsonString);
        JSONArray jsonArray = routeObject.getJSONArray("transits");
        JSONObject zuiJson = jsonArray.getJSONObject(0);
        return zuiJson.get(key).toString();
    }


    public static void main(String[] args) {
        // 起始地 经度lo - 维度 la
        Double[] origin = {104.080775,30.616258};
        // 目的地
        Double[] destination = {104.067821,30.676273};
        try {
            long st = System.currentTimeMillis();
            MapNavUtil m = new MapNavUtil(origin, destination, "8ffebc3d42b715f0329490cf0d7daf30");
            int s = Integer.parseInt(m.getDriving(GuideConstant.MAP_DISTANCE_KEY));
            System.out.println("驾车：" + s);
            System.out.println(System.currentTimeMillis() - st);
        }catch (Exception e) {
            System.out.println(e.getMessage());
            e.printStackTrace();
        }
    }
}
